<?php
    include_once('toUpperTable.php');
    include_once('printTable.php');
    include_once('checkTable.php');
    include_once('scanFile.php');
    include_once('sortTwo.php');
    function htmlReplace($str){ return str_replace("\n",'<br>',str_replace(" ",'&nbsp;',$str)); }

    $testFiles=[];
    if($argc<6)
        error_log("参数不够,需要至少5个参数");
    else if($argc==6)
    {
        $testFiles[]=$argv[4];
        $dir_handle=opendir($argv[5]);
        while(false!==($file=readdir($dir_handle)))
        {
            if($file!='.'&&$file!='..')
            {
                if(strtolower(pathinfo($argv[5].$file)['extension'])=='sql'&&$argv[5].$file!=$argv[4])
                    $testFiles[]=$argv[5].$file;
            }
        }
        closedir($dir_handle);
    }
    else
        for($i=4;$i<$argc;++$i)
            $testFiles[]=$argv[$i];
    $banCommand=['ALTER','DROP','INSERT','UPDATE','DELETE'];
    $pdo=new PDO('dblib:host='.$argv[1],$argv[2],$argv[3]);
    $testSql=[];
    foreach($testFiles as $file)
    {
        echo 'Scaning file '.$file.' ';
        $sqlA=scanFile($file,$banCommand,'--');
        $sqlB=scanFile($file,$banCommand,'/*');
        if(count($sqlA)>=count($sqlB))
        {
            echo "by --";
            $testSql[$file]=$sqlA;
        }else
        {
            echo "by /*";
            $testSql[$file]=$sqlB;
        }
        echo "\n";
    }
    $ansTable=[];

    $results=[];
    foreach($testSql as $name=>$sqls)
    {
        $results[$name]=[];
        echo "========================================================================================================================================================\n";
        echo "========================================================================================================================================================\n";
        foreach($sqls as $i=>$sql)
        {
            $results[$name][$i]=true;
            $stmt=$pdo->prepare($sql);
            $stmt->execute();
            if(strpos(strtoupper($sql),strtoupper('USE'))!==false)
            {
                echo "========================================================================================================================================================\n";
                continue;
            }
            $requestAns=$stmt->fetchAll();
            if(!array_key_exists($i,$ansTable))
            {
                printf("Set ans %24s on test case %3d\n",$name,$i);
                $ansTable[$i]=$requestAns;
            }else
            {
                $results[$name][$i]=checkTable($requestAns,$ansTable[$i]);
                if($results[$name][$i]===true)
                    printf("Passed %24s on test case %3d\n",$name,$i);
                else
                {
                    $ansTable[$i]=toUpperTable($ansTable[$i]);
                    $requestAns=toUpperTable($requestAns);
                    $results[$name][$i]=checkTable($requestAns,$ansTable[$i]);
                    if($results[$name][$i]===true)
                    {
                        printf("Passed %24s on test case %3d (after toUpper)\n",$name,$i);
                    }else
                    {
                        $results[$name][$i]=checkTable(sortTwo($requestAns),sortTwo($ansTable[$i]));
                        if($results[$name][$i]===true)
                        {
                            printf("Passed %24s on test case %3d (after toUpper sort)\n",$name,$i);
                        }else
                        {
                            echo "-----------------------------------------------------------------------------------------------\n";
                            printf("Failed %24s on test case %3d\n",$name,$i);
                            echo $sql;
                            //var_dump($results[$name][$i]);
                            if($results[$name][$i]['type']=='长度不同')
                            {
                                printTables('当前',$requestAns);
                                printTables('答案',$ansTable[$i]);
                            }else
                            {
                                printTable('当前',$requestAns[$results[$name][$i]['k1']]);
                                printTable('答案',$ansTable[$i][$results[$name][$i]['k1']]);
                            }
                            $results[$name][$i]['ans1']=$requestAns;
                            $results[$name][$i]['ans1code']=$sql;
                            $results[$name][$i]['ans2']=$ansTable[$i];
                            $results[$name][$i]['ans2code']=reset($testSql)[$i];
                            echo "-----------------------------------------------------------------------------------------------\n";
                        }
                    }
                }
            }
        }
    }


    $fp=fopen("result.html","w");
    fprintf($fp,"<html><head></head><body>");

    fprintf($fp,"<table border='1' cellspacing='0' width='100%%'>");
    foreach($results as $k=>$a)
    {
        fprintf($fp,"<tr>");
        fprintf($fp,"<td width='40%%'><a href='#%s'>%s</a></td>",$k,$k);
        $grade=100-59;
        $gradePerQuestion=1;
        foreach($a as $i=>$b)
            if($b===true)
                $grade+=$gradePerQuestion;
        fprintf($fp,"<td>%d分</td>",$grade);
        fprintf($fp,"</tr>");
    }
    fprintf($fp,"</table>");

    foreach($results as $k=>$a)
    {
        fprintf($fp,"<a name='%s'></a>",$k);

        fprintf($fp,"<table border='1' cellspacing='0' width='100%%'>");
        fprintf($fp,"<tr><td colspan='2' style='background: #00ff00;'><a href='#%s'>%s</a></td></tr>",$k,$k);

        foreach($a as $i=>$b)
        {
            fprintf($fp,"<tr>");
            fprintf($fp,"<td width='20%%'>测试点%d</td>",$i);
            if($b===true)
            {
                fprintf($fp,"<td style='color: green'>通过</td>");
            }else
            {
                fprintf($fp,"<td>");
                fprintf($fp,"<span style='color: red' onclick='event.target.nextSibling.style.display=(event.target.nextSibling.style.display==\"none\")?\"\":\"none\"'>未通过,%s(点击展开)</span>",$b['type']);

                fprintf($fp,"<table border='1' cellspacing='0' width='100%%' style='display: none;'>");

                fprintf($fp,"<tr><td width='10%%'>你的代码</td><td width='40%%'>%s</td><td width='10%%'>答案代码</td><td width='40%%'>%s</td></tr>",htmlReplace($b['ans1code']),htmlReplace($b['ans2code']));

                if($b['type']=='字段缺失'||$b['type']=='小数不同'||$b['type']=='答案不同')
                    fprintf($fp,"<tr><td width='10%%'>你的结果</td><td width='40%%'>%s</td><td width='10%%'>答案的结果</td><td width='40%%'>%s</td></tr>",printHtmlTable($b['ans1'][$b['k1']]),printHtmlTable($b['ans2'][$b['k1']]));
                fprintf($fp,"<tr><td width='10%%'>你的全部结果</td><td width='40%%'>%s</td><td width='10%%'>答案的全部结果</td><td width='40%%'>%s</td></tr>",printHtmlTables($b['ans1']),printHtmlTables($b['ans2']));

                fprintf($fp,"</table>");
                fprintf($fp,"</td>");
            }
            fprintf($fp,"</tr>");
        }

        fprintf($fp,"</table>");
    }
    fprintf($fp,"</body></html>");
    fclose($fp);

    $fp=fopen("result.csv","w");
    foreach($results as $k=>$a)
    {
        $grade=100-59;
        $gradePerQuestion=1;
        foreach($a as $i=>$b)
            if($b===true)
                $grade+=$gradePerQuestion;
        fprintf($fp,"%s,%d\n",mb_convert_encoding($k,'GB2312','UTF-8'),$grade);
    }
    fclose($fp);
